/* Fenil Chandarana Fab Academy 2022 Webpage link: https://fabacademy.org/2022/labs/vigyanashram/students/fenil-chandarana/ */ // maximum allowed delay between two presses const int max_delay = 500; int counter = 0; bool done = false; const byte ledPin1 = 5; const byte ledPin2 = 18; const byte buttonPin = 32; unsigned long first_pressed_millis = 0; void counter_incr() { counter++; } void setup() { Serial.begin(115200); pinMode(ledPin1, OUTPUT); //OUTPUT LED pinMode(ledPin2, OUTPUT); //OUTPUT LED pinMode(buttonPin, INPUT_PULLUP); //INPUT BUTTON as pullup digitalWrite(ledPin1, LOW); //THE LED IS LOW INITIALLY digitalWrite(ledPin2, LOW); //THE LED IS LOW INITIALLY attachInterrupt(digitalPinToInterrupt(buttonPin), counter_incr, RISING); } void loop() { if (counter > 0) { first_pressed_millis = millis(); // wait for user to press the button again while (millis() - first_pressed_millis < max_delay) { // if button pressed again if (counter > 1) { digitalWrite(ledPin1, HIGH); done = true; break; } } // if on timeout no button pressed it means the button pressed only one time if (!done) digitalWrite(ledPin2, HIGH); done = false; counter = 0; } }